package com.company.cloud.user.modules.mgr.controller;

import com.company.cloud.common.auth.annotation.StaffLogc;
import com.company.cloud.user.modules.api.response.PermissionModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseParam;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.StrSplitUtils;
import com.company.cloud.user.modules.base.entity.PermissionInfo;
import com.company.cloud.user.modules.base.service.PermissionInfoService;
import com.company.cloud.user.modules.cache.utils.UserConvertUtils;
import com.company.cloud.user.modules.mgr.controller.req.PermissionSaveParam;
import com.company.cloud.user.modules.mgr.controller.req.PermissionUpdateParam;
import com.company.cloud.user.modules.mgr.service.PermissionMgrService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;

@Log4j2
@Authc
@RestController
@RequestMapping("/mgr/admin/permission")
@Api(tags="User_Mgr_权限(菜单)管理")
public class PermissionMgrController {


	@Autowired
	private PermissionMgrService permissionMgrService;

	
	@PostMapping("/queryTree")
	@ApiOperation(value="权限树形列表", response= PermissionModel.class)
	public BaseResult tree(BaseParam params) {
		return BaseResult.ok(permissionMgrService.tree());
	}
	
	@GetMapping("/get")
	@ApiOperation(value="权限详情信息",response = PermissionModel.class)
	public BaseResult get(@RequestParam("code") String code) {
		PermissionModel data= permissionMgrService.get(code);
		return BaseResult.ok(data);
	}
	
	@PostMapping("/save")
	@StaffLogc(title = "User_Mgr_权限(菜单)管理 、权限新增", action = "User_Mgr_权限(菜单)管理、权限新增", actionAdo = "User_Mgr_权限(菜单)管理")
	@ApiOperation(value="权限新增", response=PermissionModel.class)
	public BaseResult save(@RequestBody PermissionSaveParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		PermissionModel data=permissionMgrService.save(params,userInfo);
		return BaseResult.ok(data);
	}
	
	
	@PostMapping("/update")
	@StaffLogc(title = "User_Mgr_权限(菜单)管理 、权限修改", action = "User_Mgr_权限(菜单)管理、权限修改", actionAdo = "User_Mgr_权限(菜单)管理")
	@ApiOperation(value="权限修改", response=PermissionModel.class)
	public BaseResult update(@RequestBody PermissionUpdateParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		PermissionModel data= permissionMgrService.update(params,userInfo);
		return BaseResult.ok(data);
	}
	
	@GetMapping("/delete")
	@StaffLogc(title = "User_Mgr_权限(菜单)管理 、权限删除", action = "User_Mgr_权限(菜单)管理、权限删除", actionAdo = "User_Mgr_权限(菜单)管理")
	@ApiOperation(value="权限删除",response=BaseResult.class)
	public BaseResult delete(@RequestParam("code") String code) {
		log.debug("删除权限,权限编号:{}", code);
		permissionMgrService.delete(code, AuthContextUtils.getUser());
		return BaseResult.ok();
	}
	
	
	
	
}
